<template>
  <div style="display: inline-block">
    <div class="top-menu">
      <div
        v-for="(item,key) in menuList"
        :key="key"
        v-if="!item.meta.hideInBread"
        class="top-menu-item"
        :class="currentTopMenu.path === item.path ? 'top-menu-active' : ''"
      >
        <div @click="chooseMenu(item)">
          <div>
            <CommonIcon :type="item.meta.icon" :size="iconSize"/>&nbsp;{{ showTitle(item) }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import mixin from '../side-menu/mixin'

export default {
  name: 'top-nav',
  mixins: [mixin],
  props: {
    menuList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      iconSize: 20,
      currentTopMenu: {}
    }
  },
  mounted () {
    if (this.$route.fullPath === '/home') {
      this.currentTopMenu = this.menuList[0]
    } else {
      this.currentTopMenu = this.$route.matched[0]
      for (let key in this.menuList) {
        if (this.menuList[key].path === this.currentTopMenu.path) {
          this.$emit('menuChange', this.menuList[key])
        }
      }
    }
  },
  methods: {
    chooseMenu: function (menu) {
      this.currentTopMenu = menu
      if (menu.redirect) {
        this.$router.push({
          path: menu.redirect
        })
        return
      }
      this.$emit('menuChange', menu)
    }
  },
  watch: {
    '$route': function (data) {
      if (!data.meta.hideInBread) {
        // this.currentTopMenu = data.matched[0]
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.top-menu {
  margin-left: 20px;
  margin-top: 9px;
  line-height: 30px;

  .top-menu-item {
    //background: #ffffff;
    //width: 80px;
    float: left;
    text-align: center;
    color: #666;
    font-size: 14px;
    font-family: PingFang SC, Arial, Microsoft YaHei, sans-serif;
    font-weight: 500;
    height: 40px;
    margin-top: 5px;
    padding-top: 0;
    cursor: pointer;
    line-height: 40px;
    .ivu-icon{
      margin-top: -4px;
    }

    .top-menu-icon {
      font-size: 20px;
    }
  }
  .top-menu-item:nth-of-type(odd){
    border-right: 1px solid #c8c8c8;
    padding-right: 30px;
    margin-left: 20px;
    //width: 100px;
  }
  .top-menu-item:nth-of-type(even){
    margin-left: 30px;
  }
  .top-menu-item:last-child{
    border-right: none;
  }
  .top-menu-item:hover{
    color: rgb(67 143 224);
  }

  .top-menu-active {
    color: rgb(67 143 224);
  }
}
</style>
